﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Browser;
using System.IO;
using System.Windows.Resources;
using System.Windows.Media.Imaging;
using System.Collections.ObjectModel;
using ShapeDraw.Control; 
namespace ShapeDraw
{
    public partial class MainPage : UserControl
    {
        ObservableCollection<Service.FloorModel> floors;
        ObservableCollection<Service.PavilionModel> pavilions; 
        public MainPage()
        {
          
            InitializeComponent();
            InitData(); 
            LoadImg();
            CreatMenu();
           // this.LayoutUpdated += new EventHandler(MainPage_LayoutUpdated);

            //Pavilion p1 = new Pavilion("北京展馆1","1");
            //Pavilion p2 = new Pavilion("北京展馆2", "2");
            //pavilions.Add(p1);
            //pavilions.Add(p2);
            //cboxZhan.ItemsSource = pavilions;
            //cboxZhan.SelectedValue = 1;
            //cboxZhan.DisplayMemberPath = "PName";
           
            //Floor first = new Floor("第一层", "/ShapeDraw;component/Images/1.jpg",1);
            //Floor second = new Floor("第二层", "/ShapeDraw;component/Images/2.jpg",1);
            //Floor third = new Floor("第三层", "/ShapeDraw;component/Images/3.jpg",2);
            //floors.Add(first);
            //floors.Add(second);
            //floors.Add(third);
            //cbox.DisplayMemberPath = "FName";
            //cbox.ItemsSource = floors;

        }
        /// <summary>
        /// 初始化展馆和楼层
        /// </summary>
        private void InitData()
        {
            //Scroll.Width = App.Current.Host.Content.ActualWidth;
            //Scroll.Height = App.Current.Host.Content.ActualHeight - 80;
            //Scroll.Margin = new Thickness(0, 80, 0, 0);
            ////master.Margin = new Thickness(0, 100, 0, 0);
            //master.Width = Scroll.Width - 100;
            //master.Height = Scroll.Height;
            //Service.CommServiceClient service = Server.ServiceHelper.InitService();
            //service.GetPavilionListAsync();
            //service.GetPavilionListCompleted += new EventHandler<Service.GetPavilionListCompletedEventArgs>(GetPavilionListCompleted);

            //var memberService = ShapeDraw.Server.ServiceHelper.WcfServiceClientFactory<Service.CommServiceClient,
            //        MemberService>.CreateServiceClient();
        }

        private void GetPavilionListCompleted(object sender, Service.GetPavilionListCompletedEventArgs e)
        {
            if (e.Result != null) {
                pavilions = (ObservableCollection<Service.PavilionModel>)e.Result;
                cboxZhan.DisplayMemberPath = "PaName";
                cboxZhan.ItemsSource = pavilions; 
            }
        }

        
        
        #region menu
        public void CreatMenu() {

            ContextMenu cm = new ContextMenu();
            MenuItem m1 = new MenuItem();
            m1.Header = "复制";
            m1.Click += new RoutedEventHandler(Copy_Click);
            cm.Items.Add(m1);

            MenuItem m2 = new MenuItem();
            m2.Header = "修改";
            m2.Click += new RoutedEventHandler(Modify_Click);
            cm.Items.Add(m2);

            ContextMenuService.SetContextMenu(this.Img, cm);

        }

        private void Modify_Click(object sender, RoutedEventArgs e)
        {
             
        }

        private void Copy_Click(object sender, RoutedEventArgs e)
        {
             
        }
        #endregion
        //private double _minimalHeight = 300;

        void MainPage_LayoutUpdated(object sender, EventArgs e)
        {

            Scroll.Width = App.Current.Host.Content.ActualWidth;
            Scroll.Height = App.Current.Host.Content.ActualHeight - 80;
            Scroll.Margin = new Thickness(0, 80, 0, 0);
            //master.Margin = new Thickness(0, 100, 0, 0);
            master.Width = Scroll.Width - 100;
            master.Height = Scroll.Height;
            
            

        }


        private void LoadImg()
        {
            try
            {
                
                
                
                //var uri=new Uri("/ShapeDraw;"+ LoadImage("/ShapeDraw;component/Images/1.jpg"),UriKind.Relative);

                //master.Background = new ImageBrush() { ImageSource = new BitmapImage(uri) };

                Img.Source = new BitmapImage(new Uri("/ShapeDraw;component/Images/1.jpg",UriKind.Relative));
                if (HtmlPage.Document.QueryString.Count > 0)
                {
                    var query = HtmlPage.Document.QueryString["ID"];
                   // master.Background = new ImageBrush() { ImageSource = new BitmapImage(new Uri("/ShapeDraw;component/Images/"+query+".jpg", UriKind.Relative)) };
                   

                }
            }
            catch (Exception e) {
                MessageBox.Show(e.Message);
            }
        }

       


        private void DPolyLine_PolylineEndDrawed(object sender, DrawEventArgs e)
        {
            StandChild cld = new StandChild();
            cld.Show();
            //string s = "";
            //foreach(Point p in e.Points)
            //{
            //    s += string.Format("{0},{1} ",p.X,p.Y);
            //}
            
            //MessageBox.Show(s);
        }

        private void DPolyLine_PointDrawed(object sender, DrawEventArgs e)
        {
            
        }




        #region 放大缩小元素
        //private void LayoutRoot_MouseWheel(object sender, MouseWheelEventArgs e)       
        //{            
        //    // e.Delta滚动一个齿轮为120，向前为正，向后为负所以120/1200，让增量变到正负0.1  
        //    //double delta = e.Delta / 1200.0;          
        //    //canvasTrans.ScaleX += delta;        
        //    //canvasTrans.ScaleY += delta;     
        //}       
        //#endregion

        

        //private void master_MouseMove(object sender, MouseEventArgs e)
        //{
        //    //如果鼠标处理按下状态

        //    if (isMouseDown == true)
        //    {

        //        //获取鼠标当前位置

        //        double currX = e.GetPosition(null).X;

        //        double currY = e.GetPosition(null).Y;

        //        //获取当前图形的位置

        //        double currLeft = (double)((UIElement)sender).GetValue(Canvas.LeftProperty);

        //        double currTop = (double)((UIElement)sender).GetValue(Canvas.TopProperty);

        //        //移动当前图形的位置

        //        ((UIElement)sender).SetValue(Canvas.LeftProperty, currLeft + currX - beginX);

        //        ((UIElement)sender).SetValue(Canvas.TopProperty, currTop + currY - beginY);

        //        //将当前位置保存起来，继续拖动。

        //        beginX = currX;

        //        beginY = currY;

        //    }

        //}

        //private void master_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        //{
        //    //将当前的鼠标位置传给私有变量

        //    beginX = e.GetPosition(null).X;

        //    beginY = e.GetPosition(null).Y;

        //    //将isMouseDown设为true表示鼠标按下。

        //    isMouseDown = true;

        //    //由于CaptureMouse都定义在UIElement中，所以这里、转换为UIElement类

        //    FrameworkElement fe = sender as FrameworkElement;
        
        //    if (fe != null)
        //    {
        //        fe.CaptureMouse();
        //        fe.Cursor = Cursors.Hand;
        //    }



        //}

        //private void master_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        //{
        //    isMouseDown = false;

        //    //停止鼠标捕捉
        //    FrameworkElement fe = sender as FrameworkElement;

        //    fe.ReleaseMouseCapture();


        //}
        #endregion

        private void btn_Un_Click(object sender, RoutedEventArgs e)
        {
            DPolyLine p = new DPolyLine();
            master.Children.Add(p);
        }

        private void btn_Position_Click(object sender, RoutedEventArgs e)
        {
            DPolyLine p = new DPolyLine();
            p.PolylineEndDrawed += new EventHandler<DrawEventArgs>(DPolyLine_PolylineEndDrawed);
            master.Children.Add(p);
        }
        private void cboxZhan_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            //Pavilion obj = ((ComboBox)sender).SelectedItem as Pavilion;
            //var selID =Convert.ToInt32( obj.PlValue);
             
            //if (obj != null) {
            //  var  fls = floors.Where(s => s.Fid == selID).ToList();
            //  //cbox.DisplayMemberPath = "FName";
            //  cbox.ItemsSource = fls;
            //}

        }


        private void sliderImg_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            if (e.NewValue > e.OldValue)
            {
                //Scroll.Width += e.NewValue + 100;
                //Scroll.Height += e.NewValue + 100;
                Img.Width +=  10;
                Img.Height +=  10;
            }
            else {
                //Scroll.Width += e.NewValue - 100;
                //Scroll.Height += e.NewValue - 100;
                Img.Width +=  - 10;
                Img.Height +=  - 10;
            }
        }

        private void btnSetColor_Click(object sender, RoutedEventArgs e)
        {
            ColorChild cld = new ColorChild();
            cld.Show();
           
        }

        private void cbox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            Floor obj = ((ComboBox)sender).SelectedItem as Floor;
            if (obj != null)
            {
                Img.Source = new BitmapImage(new Uri(obj.FlValue, UriKind.Relative));
            }
        }



    }
}
